feat(countsketch-topk): add portable CountSketchWithHeap wire type#78
Merged
Conversation
Fixes a real correctness gap surfaced while implementing
ASAPQuery-backend's SummaryExecutor (data_plane): the only
heap-bearing frequency wire type this crate exposed was
CountMinSketchWithHeap (portable::countminsketch_topk). data_plane's
query-side reducer reused that same type for BOTH CmsWithHeap and
CountSketchWithHeap sids -- meaning every CountSketchWithHeap query was
being read out with CMS's min-over-rows estimator instead of Count
Sketch's own median-of-signed-rows estimator. The sketches::CSHeap
computational type (median estimator, "mirrors CMSHeap but with Count
Sketch") already existed and was already correct; there was just no
portable/wire-format wrapper for it, unlike every other sketch family.
Adds `message_pack_format::portable::countsketch_topk::CountSketchWithHeap`,
structurally mirroring CountMinSketchWithHeap (same wire shape: {sketch:
{sketch, row_num, col_num}, topk_heap: [{key, value}], heap_size}) but
backed by CSHeap instead of CMSHeap. The wire SHAPE is unchanged from
what CountMinSketchWithHeap already produces/consumes for this data --
this is a new Rust-side reader/builder for the same bytes with correct
math, not a new cross-language protocol; sketchlib-go needs no changes.
Also adds `#[derive(Clone)]` to `sketches::CSHeap` (CMSHeap already had
it; CSHeap was missing it, needed for CountSketchWithHeap::merge_refs's
structural clone -- both of CSHeap's own fields, Count and HHHeap, are
already Clone).
## Test plan
- [x] `cargo test message_pack_format::portable::countsketch_topk` -- 8
new tests, all passing: creation, empty query, merge (+ dimension
mismatch), msgpack round-trip, aggregate_topk (+ empty), and
test_median_estimator_differs_from_cms_style_min -- the actual
point of this addition, proving the median estimator behaves
differently from a CMS-style min estimator on a skewed insert
pattern, not just sharing a struct shape.
- [x] `cargo test` (full workspace) -- 466 unit + 5 asapv1_golden + 12
msgpack_compat (5 ignored) + 4 sketches_go_parity_probe + 1
xtest_consumer + 19 doctests, all passing.
- [x] `cargo clippy --all-targets --all-features -- -D warnings` --
clean.
- [x] `cargo fmt --check` -- clean.
Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Fixes a real correctness gap surfaced while implementing ASAPQuery-backend's
SummaryExecutor(data_plane, see ASAPQuery-backend#411): the only heap-bearing frequency wire type this crate exposed wasCountMinSketchWithHeap(portable::countminsketch_topk).data_plane's query-side reducer reused that same type for bothCmsWithHeapandCountSketchWithHeapsids — meaning everyCountSketchWithHeapquery was being read out with CMS's min-over-rows estimator instead of Count Sketch's own median-of-signed-rows estimator. Thesketches::CSHeapcomputational type (median estimator, its own doc already says "mirrorsCMSHeapbut with Count Sketch") already existed and was already correct — there was just no portable/wire-format wrapper for it, unlike every other sketch family in this crate.Adds
message_pack_format::portable::countsketch_topk::CountSketchWithHeap, structurally mirroringCountMinSketchWithHeap(same wire shape:{sketch: {sketch, row_num, col_num}, topk_heap: [{key, value}], heap_size}) but backed byCSHeapinstead ofCMSHeap.Not a new cross-language protocol surface. The wire shape is unchanged from what
CountMinSketchWithHeapalready produces/consumes for this data (I copied its wire struct verbatim) — this is a new Rust-side reader/builder for the same bytes with correct math, not a new formatsketchlib-goneeds to learn. No Go-side changes needed.Also adds
#[derive(Clone)]tosketches::CSHeap(CMSHeapalready had it;CSHeapwas missing it — needed forCountSketchWithHeap::merge_refs's structural clone; both ofCSHeap's own fields,CountandHHHeap, are alreadyClone, so this was just an oversight, not a design constraint).Test plan
cargo test message_pack_format::portable::countsketch_topk— 8 new tests, all passing: creation, empty query, merge (+ dimension mismatch), msgpack round-trip,aggregate_topk(+ empty), andtest_median_estimator_differs_from_cms_style_min— the actual point of this addition, proving the median estimator behaves differently from a CMS-style min estimator on a skewed insert pattern, not just sharing a struct shape.cargo test(full workspace) — 466 unit + 5asapv1_golden+ 12msgpack_compat(5 ignored) + 4sketches_go_parity_probe+ 1xtest_consumer+ 19 doctests, all passing.cargo clippy --all-targets --all-features -- -D warnings— clean.cargo fmt --check— clean.🤖 Generated with Claude Code